package com.talent.jiwen.my;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.talent.jiwen.R;
import com.talent.jiwen.base.BaseActivity;
import com.talent.jiwen.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen.http.common.ApiFactory;
import com.talent.jiwen.http.common.HttpUtil;
import com.talent.jiwen.http.common.ProgressSubscriber;
import com.talent.jiwen.http.result.EvaluateListResult;
import com.talent.jiwen.teacher.TeacherDetailActivity;
import com.talent.jiwen.ui.widgets.StarBar;
import com.talent.jiwen.util.DateUtils;
import com.talent.jiwen.util.ImageUtil;
import com.talent.jiwen.util.SPConstant;
import com.talent.jiwen.util.SPUtil;
import com.talent.jiwen.util.Validators;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import rx.Observable;

public class MyEvaluateActivity extends BaseActivity {
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.noDataIv)
    ImageView noDataIv;

    private int startIndex = 1;
    private List<EvaluateListResult.Evaluate> dataList = new ArrayList<>();

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_my_evaluate;
    }

    @Override
    public String titleName() {
        return getString(R.string.my_evaluate);
    }

    @Override
    protected void initData() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recycleView.setLayoutManager(layoutManager);
        recycleView.setAdapter(new CommonAdapter<EvaluateListResult.Evaluate>(this , R.layout.item_my_evaluate , dataList){
            @Override
            protected void convert(ViewHolder holder, final EvaluateListResult.Evaluate evaluate, int position) {
                ImageView headIv = holder.getView(R.id.headIv);
                TextView nameTv  = holder.getView(R.id.nameTv);
                TextView timeTv = holder.getView(R.id.timeTv);
                StarBar severRatingBar = holder.getView(R.id.severRatingBar);
                StarBar effectRatingBar = holder.getView(R.id.effectRatingBar);
                TextView resultTv = holder.getView(R.id.resultTv);
                TextView contentTv = holder.getView(R.id.contentTv);
                RelativeLayout teacherLayout = holder.getView(R.id.teacherLayout);
                LinearLayout starLayout = holder.getView(R.id.starLayout);

                ImageUtil.loadCorner4Image(MyEvaluateActivity.this , evaluate.getTeacherHeadImage() , headIv);
                nameTv.setText(evaluate.getTeacherName());
                timeTv.setText(DateUtils.ms2Date(evaluate.getCreateTime()));

                if (evaluate.getSolveStatus() == 1){
                    resultTv.setText("已解决");
                    starLayout.setVisibility(View.VISIBLE);
                    severRatingBar.setCanTeach(false);
                    severRatingBar.setStarMark(Float.parseFloat(evaluate.getServeScore()));
                    effectRatingBar.setCanTeach(false);
                    effectRatingBar.setStarMark(Float.parseFloat(evaluate.getEffectScore()));
                }else{
                    resultTv.setText("未解决");
                    starLayout.setVisibility(View.GONE);
                }
                contentTv.setText(evaluate.getEvaluationMessage());

                teacherLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        TeacherDetailActivity.startTeacherDetailActivity(MyEvaluateActivity.this , TeacherDetailActivity.TYPE_ASK_PROBLEM
                                , evaluate.getTeacherId() , "");
                    }
                });

            }
        });

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                startIndex = 1;
                getEvaluateList();
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                startIndex ++ ;
                getEvaluateList();
            }
        });


        refreshLayout.autoRefresh();
    }

    private void getEvaluateList(){
        Map<String, String> map = new HashMap<>();
        map.put("studentId" , SPUtil.stringOut(SPConstant.USER_ID));
        map.put("startIndex" , startIndex + "");

        Observable observable = ApiFactory.getEducationApiSingleton().getStudentEvaluationList(SPUtil.stringOut(SPConstant.TOKEN) , map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<EvaluateListResult>(this) {
            @Override
            protected void _onNext(EvaluateListResult result) {
                if(startIndex == 1){
                    dataList.clear();
                    dataList.addAll(result.getEvaluationList());
                    recycleView.getAdapter().notifyDataSetChanged();
                    refreshLayout.finishRefresh();
                }else{
                    dataList.addAll(result.getEvaluationList());
                    recycleView.getAdapter().notifyDataSetChanged();
                    refreshLayout.finishLoadMore();

                    if (result.getEvaluationList().size() < 10){
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    }
                }

                if (Validators.isEmpty(dataList)){
                    noDataIv.setVisibility(View.VISIBLE);
                }else{
                    noDataIv.setVisibility(View.GONE);
                }
            }

            @Override
            protected void _onError(String message) {
                showToast(message);
                refreshLayout.finishRefresh();
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject,false);
    }
}
